<?php
require_once('opendb.php');
require_once('weight.php');

class weightManager
{
    public static function addWeight($weight)
    {
        $weight = mysql_real_escape_string($weight);
        $courseID = $weight->getCourseID();
        $type = $weight->getType();
        $percent = $weight->getPercent();
        $insert = mysql_query("INSERT INTO course_gradingscale VALUES('$courseID', NULL, '$type', '$percent')");
        $num = mysql_affected_rows();
        if ($num == 1)
        {
            $select = mysql_query("SELECT LAST_INSERT_ID()");
            $result = mysql_num_rows($select);
            $weightID = $result['GradingID'];
            $weight->setWeightID($weightID);
            echo "The weight has been added successfully.";
            return $weight;
        } else
        {
            echo "The weight has not been added successfully.";
        }
    }

    public static function editWeight($weight)
    {
        $weight = mysql_real_escape_string($weight);
        $weightID = $weight->getWeightID();
        $courseID = $weight->getCourseID();
        $type = $weight->getType();
        $percent = $weight->getPercent();
        $update = mysql_query("UPDATE course_gradingscale SET CourseID = '$courseID', Type = '$type', Percentage = '$percent' WHERE WeightID = '$weightID' LIMIT 1");
        $num = mysql_affected_rows();
        if ($num == 1)
            echo "The weight has been successfully updated.";
        else
            echo "The weight has not been successfully updated.";
    }

    public static function deleteWeight($weight)
    {
        $weight = mysql_real_escape_string($weight);
        $weightID = $weight->getWeightID();
        $delete = mysql_query("DELETE FROM course_gradingscale WHERE GradingID = '$weightID' LIMIT 1");
        $num = mysql_affected_rows();
        if ($num == 1)
            echo "The weight has been successfully deleted.";
        else
            echo "The weight has not been successfully deleted.";

    }

    public static function getWeights($courseID)
    {
        $courseID = mysql_real_escape_string($courseID);
        $select = mysql_query("SELECT * FROM course_gradingscale WHERE CourseID = '$courseID'");
        $weightArray = array();
        while ($row = mysql_fetch_array($select))
        {
            $weight = new Weight();
            $weight = weightManager::getWeight($row['GradingID']);
            $weightArray[] = $weight;
        }
        return $weightArray;
    }


    public static function getWeight($weightID)
    {
        $weightID = mysql_real_escape_string($weightID);
        $select = mysql_query("SELECT * FROM course_gradingscale WHERE GradingID = '$weightID'");
        $result = mysql_num_rows($select);
        if ($result > 0)
        {
            $weightInfo = mysql_fetch_array($select);
            $weight = new Weight();
            $weight->setCourseID($weightInfo['CourseID']);
            $weight->setWeightID($weightInfo['WeightID']);
            $weight->setType($weightInfo['Type']);
            $weight->setPercent($weightInfo['Percentage']);
            return $weight;
        } else
            echo "There is no weight with that ID.";
    }
}
?>